<template>
	<div id="news">
		<div id="header">
			<img @click="type == 0?$router.go(-1):(type = 0,newsTitle = '消息')" src="../../static/img/return.png"/>
			<p>{{newsTitle}}</p>
		</div>
		<!-------消息类型----------->
		<div v-if="type == 0" class="news_type">
			<div @click="type = 1, getNotice()" class="new_type">
				<div class="new_left">
					<img src="../../static/img/news-01.png"/>
					<p>系统公告</p>
				</div>
				<div class="new_right">
					<img src="../../static/img/news-04.png"/>
				</div>
			</div>
			<div class="news_2">
				<div @click="type = 2, getSysMsg()" class="new_type">
					<div class="new_left">
						<img src="../../static/img/news-02.png"/>
						<p>系统消息</p>
						<span v-if="flowNum > 0"></span>
					</div>
					<div class="new_right">
						<img src="../../static/img/news-04.png"/>
					</div>
				</div>
				<div @click="type = 3, getTaskMsg()" class="new_type">
					<div class="new_left">
						<img src="../../static/img/news-03.png"/>
						<p>活动消息</p>
						<span v-if="taskNum > 0"></span>
					</div>
					<div class="new_right">
						<img src="../../static/img/news-04.png"/>
					</div>
				</div>
			</div>
		</div>
		<!-------消息列表----------->
		<div v-else class="news_list">
			<div @click="detail = true, msgdetail(item)" class="new_item" v-for="item in msgList">
				<p class="new_title">{{type==1?item.noticeTitle:item.title}}</p>
				<div class="new_content">
					<p>{{item.content}}</p>
				</div>
				<p class="new_time">{{getDate(item.createtime)}}</p>
			</div>
		</div>
		<!--------消息详情弹窗------------>
		<div @click="detail = false" v-show="detail" id="new_detail">
			<div @click.stop="" class="new_detail">
				<p class="new_title">{{type==1?msg.noticeTitle:msg.title}}</p>
				<div class="new_content">
					<p>{{msg.content}}</p>
				</div>
				<p class="new_time">{{getDate(msg.createtime)}}</p>
			</div>
		</div>
	</div>
</template>

<script>
export default{
	name: 'news',
	data:function(){
		return{
			newsTitle: '消息',//header名称
			type: 0,//消息类型
			detail: false,//消息详情弹窗开关
			msgList: [], // 消息列表
			msg: {}, // 详情
			taskNum: 0, // 活动消息数量
			flowNum: 0, // 流水消息数量
		}
	},
	methods:{
		//----获取公告列表----
		getNotice: function(){
			var _this = this;
			_this.newsTitle = '系统公告';
			_this.$axios.post("/user/notice/public/selectNoticeList").then(function(res){
				var data = res.data;
				if(data.message == "succ"){
					_this.msgList = data.result;
				}else{
					console.log(data.result);
				}
			}).catch(function(err){
				console.log(err);
			});
		},
		//----获取系统消息----
		getSysMsg: function(){
			var _this = this;
			_this.newsTitle = '系统消息';
			let postData = _this.$qs.stringify({
				type: 2
			});
			_this.$axios.post("/user/message/getMessageList", postData).then(function(res){
				var data = res.data;
				if(data.message == "succ"){
					_this.msgList = data.result.list;
					_this.readAllMsg(2);
				}else{
					console.log(data.result);
				}
			}).catch(function(err){
				console.log(err);
			});
		},
		//----获取活动消息----
		getTaskMsg: function(){
			var _this = this;
			_this.newsTitle = '活动消息';
			let postData = _this.$qs.stringify({
				type: 1
			});
			_this.$axios.post("/user/message/getMessageList", postData).then(function(res){
				var data = res.data;
				if(data.message == "succ"){
					_this.msgList = data.result.list;
					_this.readAllMsg(1);
				}else{
					console.log(data.result);
				}
			}).catch(function(err){
				console.log(err);
			});
		},
		//--------日期转换-----------
		getDate: function(str){  
			if(str == ""||str ==null){
				return "";
			}
		    var oDate = new Date(str),  
		    oYear = oDate.getFullYear(),  
		    oMonth = oDate.getMonth()+1,  
		    oDay = oDate.getDate(),  
		    oHour = oDate.getHours(),  
		    oMin = oDate.getMinutes(),  
		    oSen = oDate.getSeconds(),  
		    oTime = oYear +'-'+ this.getzf(oMonth) +'-'+ this.getzf(oDay) +' '+ this.getzf(oHour) +':'+ this.getzf(oMin) +':'+this.getzf(oSen);//最后拼接时间  
		    return oTime;  
		},
		//补0操作  
		getzf:function(num){  
		    if(parseInt(num) < 10){  
		        num = '0'+num;  
		    }  
		    return num;  
		},
		// 详情
		msgdetail: function(item){
			this.msg = item;
			// 如果是公告 增加阅读数
			if(this.type == 1){
				var _this = this;
				let postData = _this.$qs.stringify({
					noticeId: item.id
				});
				_this.$axios.post("/user/notice/public/noticeDetail", postData).then(function(res){
					var data = res.data;
					if(data.message == "succ"){
						
					}else{
						console.log(data.result);
					}
				}).catch(function(err){
					console.log(err);
				});
			}
		},
		// 将同一type的消息变为已读
		readAllMsg: function(type){
			var _this = this;
			let postData = _this.$qs.stringify({
				type: type
			});
			_this.$axios.post("/user/message/readAllMessage",postData).then(function(res){
				var data = res.data;
				_this.getFlowMsgNum();
				_this.getTaskMsgNum();
			}).catch(function(err){
				console.log(err);
			});
		},
		// 获取不同类型 的未读消息数量
		getFlowMsgNum: function(){
			var _this = this;
			let postData = _this.$qs.stringify({
				type: 2
			});
			_this.$axios.post("/user/message/getNotReadCount",postData).then(function(res){
				var data = res.data;
				if(data.message == "succ"){
					_this.flowNum = data.result;
				}else{
					console.log(data.result);
				}
			}).catch(function(err){
				console.log(err);
			});
		},
		getTaskMsgNum: function(){
			var _this = this;
			let postData = _this.$qs.stringify({
				type: 1
			});
			_this.$axios.post("/user/message/getNotReadCount",postData).then(function(res){
				var data = res.data;
				if(data.message == "succ"){
					_this.taskNum = data.result;
				}else{
					console.log(data.result);
				}
			}).catch(function(err){
				console.log(err);
			});
		},
	},
	watch: {
		type(){
			if(this.type == 0){
				this.msgList = [];
			}
		}
	},
	created: function(){
		this.getFlowMsgNum();
		this.getTaskMsgNum();
	}
}
</script>

<style scoped>
/* ========== */
/* = Header = */
/* ========== */
#header{
	width: 100%;
	background-color: #fff;
	position: fixed;
	top: 0;
	left: 0;
	height: 4rem;
	display: flex;
	align-items: center;
	justify-content: center;
	border-bottom: 1px solid #EEEEEE;
	font-size: 1.6rem;
	color: #4F4F4F;
	box-sizing: border-box;
	z-index: 10;
}
#header img{
	height: 2rem;
	display: block;
	position: absolute;
	top: 1rem;
	left: 2rem;
}
#news{
	height: 100%;
	padding: 5rem 0;
	box-sizing: border-box;
}
#news .new_type{
	display: flex;
	justify-content: space-between;
	align-items: center;
	padding: 0 1.5rem;
	box-sizing: border-box;
	background-color: #fff;
	height: 4rem;
	border-top: 1px solid #EEEEEE;
	border-bottom: 1px solid #EEEEEE;
}
#news .new_type>img{
	height: 1.4rem;
	display: block;
}
#news .new_type .new_left{
	display: flex;
	align-items: center;
	font-size: 1.2rem;
	color: #4F4F4F;
}
#news .new_type .new_left img{
	height: 2.6rem;
	display: block;
	margin-right: 1rem;
}
#news .new_type .new_left p{
	white-space: nowrap;
}
#news .new_type .new_right{
	display: flex;
	align-items: center;
}
#news .new_type .new_left span{
	height: 0.6rem;
    width: 0.6rem;
    display: block;
    border-radius: 50%;
    background-color: #FF464E;
    margin-left: 0.2rem;
    margin-bottom: 0.6rem;
}
#news .new_type .new_right img{
	height: 1.4rem;
	display: block;
}
.news_2{
	margin-top: 1rem;
}
#news .news_2 div.new_type:first-child{
	border-bottom: none;
}
/*----------消息列表-----------*/
.new_item{
	width: 94%;
	margin: 0 auto;
	background-color: #fff;
	border-radius: 3px;
	padding: 0 0.5rem;
	box-sizing: border-box;
	margin-bottom: 1rem;
}
.new_title{
	height: 2.4rem;
	line-height: 2.4rem;
	border-bottom: 1px solid #EEEEEE;
	font-size: 1.2rem;
	color: #4F4F4F;
}
.new_item .new_content{
	margin: 0.5rem 0;
	padding: 0 1rem;
	height: 4.5rem;
	box-sizing: border-box;
	font-size: 1.2rem;
	color: #787878;
	line-height: 1.5rem;
	overflow: hidden;
}
.new_content span{
	color: #FF6565;
}
.new_time{
	display: flex;
	justify-content: flex-end;
	align-items: center;
	height: 2.4rem;
	line-height: 2.4rem;
	font-size: 1.2rem;
	color: #A9A9A9;
	border-top: 1px solid #EEEEEE;
}
#new_detail{
	width: 100%;
	height: 100%;
	position: fixed;
	top: 0;
	left: 0;
	background-color: rgba(0,0,0,0.7);
	display: flex;
	align-items: center;
	justify-content: center;
	z-index: 11;
}
.new_detail{
	width: 94%;
	margin: 0 auto;
	background-color: #fff;
	border-radius: 3px;
	padding: 0 0.5rem;
	box-sizing: border-box;
	margin-top: -10%;
}
.new_detail .new_content{
	margin: 0.5rem 0;
	padding: 0 1rem;
	box-sizing: border-box;
	font-size: 1.2rem;
	color: #787878;
	line-height: 1.5rem;
}
</style>
